
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>算法 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Foundation.html" />
    
    
    <link rel="prev" href="Data-structure.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    关于
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Part I</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="Data-structure.html">
            
                <a href="Data-structure.html">
            
                    
                    数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.2" data-path="Arithmetic.html">
            
                <a href="Arithmetic.html">
            
                    
                    算法
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Part II</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="Foundation.html">
            
                <a href="Foundation.html">
            
                    
                    Foundation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="UIKit.html">
            
                <a href="UIKit.html">
            
                    
                    UIKit
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="WebView.html">
            
                <a href="WebView.html">
            
                    
                    WebView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="Memory-management.html">
            
                <a href="Memory-management.html">
            
                    
                    内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="Message-passing.html">
            
                <a href="Message-passing.html">
            
                    
                    消息传递的方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="Network.html">
            
                <a href="Network.html">
            
                    
                    网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="Data-storage.html">
            
                <a href="Data-storage.html">
            
                    
                    数据存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="Multi-thread.html">
            
                <a href="Multi-thread.html">
            
                    
                    多线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="Animation.html">
            
                <a href="Animation.html">
            
                    
                    动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="Image-processing.html">
            
                <a href="Image-processing.html">
            
                    
                    图像处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.11" data-path="Data-encryption.html">
            
                <a href="Data-encryption.html">
            
                    
                    数据安全及加密
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.12" data-path="Runtime.html">
            
                <a href="Runtime.html">
            
                    
                    Runtime
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.13" data-path="Runloop.html">
            
                <a href="Runloop.html">
            
                    
                    Runloop
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Part III</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="Project-organization.html">
            
                <a href="Project-organization.html">
            
                    
                    项目架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="Design-patterns.html">
            
                <a href="Design-patterns.html">
            
                    
                    设计模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="Component-based.html">
            
                <a href="Component-based.html">
            
                    
                    组件化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="Debug-tips.html">
            
                <a href="Debug-tips.html">
            
                    
                    调试技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="Performance-optimization.html">
            
                <a href="Performance-optimization.html">
            
                    
                    性能优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="Source-code.html">
            
                <a href="Source-code.html">
            
                    
                    源码理解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="Code-management.html">
            
                <a href="Code-management.html">
            
                    
                    代码管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="Continuous-integration.html">
            
                <a href="Continuous-integration.html">
            
                    
                    持续集成
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >算法</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7B97;&#x6CD5;">&#x7B97;&#x6CD5;</h1>
<h2 id="1&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;">1.&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;</h2>
<ul>
<li><p>&#x65F6;&#x95F4;&#x9891;&#x5EA6;</p>
<p>  &#x4E00;&#x4E2A;&#x7B97;&#x6CD5;&#x6267;&#x884C;&#x6240;&#x8017;&#x8D39;&#x7684;&#x65F6;&#x95F4;,&#x4ECE;&#x7406;&#x8BBA;&#x4E0A;&#x662F;&#x4E0D;&#x80FD;&#x7B97;&#x51FA;&#x6765;&#x7684;,&#x5FC5;&#x987B;&#x4E0A;&#x673A;&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x624D;&#x80FD;&#x77E5;&#x9053;.&#x4F46;&#x6211;&#x4EEC;&#x4E0D;&#x53EF;&#x80FD;&#x4E5F;&#x6CA1;&#x6709;&#x5FC5;&#x8981;&#x5BF9;&#x6BCF;&#x4E2A;&#x7B97;&#x6CD5;&#x90FD;&#x4E0A;&#x673A;&#x6D4B;&#x8BD5;,&#x53EA;&#x9700;&#x77E5;&#x9053;&#x54EA;&#x4E2A;&#x7B97;&#x6CD5;&#x82B1;&#x8D39;&#x7684;&#x65F6;&#x95F4;&#x591A;,&#x54EA;&#x4E2A;&#x7B97;&#x6CD5;&#x82B1;&#x8D39;&#x7684;&#x65F6;&#x95F4;&#x5C11;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;.&#x5E76;&#x4E14;&#x4E00;&#x4E2A;&#x7B97;&#x6CD5;&#x82B1;&#x8D39;&#x7684;&#x65F6;&#x95F4;&#x4E0E;&#x7B97;&#x6CD5;&#x4E2D;&#x8BED;&#x53E5;&#x7684;&#x6267;&#x884C;&#x6B21;&#x6570;&#x6210;&#x6B63;&#x6BD4;&#x4F8B;,&#x54EA;&#x4E2A;&#x7B97;&#x6CD5;&#x4E2D;&#x8BED;&#x53E5;&#x6267;&#x884C;&#x6B21;&#x6570;&#x591A;,&#x5B83;&#x82B1;&#x8D39;&#x65F6;&#x95F4;&#x5C31;&#x591A;.&#x4E00;&#x4E2A;&#x7B97;&#x6CD5;&#x4E2D;&#x7684;&#x8BED;&#x53E5;&#x6267;&#x884C;&#x6B21;&#x6570;&#x79F0;&#x4E3A;&#x8BED;&#x53E5;&#x9891;&#x5EA6;&#x6216;&#x65F6;&#x95F4;&#x9891;&#x5EA6;.&#x8BB0;&#x4E3A;T(n).</p>
</li>
<li><p>&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;</p>
<p>  &#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;,&#x7B97;&#x6CD5;&#x4E2D;&#x57FA;&#x672C;&#x64CD;&#x4F5C;&#x91CD;&#x590D;&#x6267;&#x884C;&#x7684;&#x6B21;&#x6570;&#x662F;&#x95EE;&#x9898;&#x89C4;&#x6A21;n&#x7684;&#x67D0;&#x4E2A;&#x51FD;&#x6570;,&#x7528;T(n)&#x8868;&#x793A;,&#x82E5;&#x6709;&#x67D0;&#x4E2A;&#x8F85;&#x52A9;&#x51FD;&#x6570;f(n),&#x4F7F;&#x5F97;&#x5F53;n&#x8D8B;&#x8FD1;&#x4E8E;&#x65E0;&#x7A77;&#x5927;&#x65F6;,T&#xFF08;n)/f(n)&#x7684;&#x6781;&#x9650;&#x503C;&#x4E3A;&#x4E0D;&#x7B49;&#x4E8E;&#x96F6;&#x7684;&#x5E38;&#x6570;,&#x5219;&#x79F0;f(n)&#x662F;T(n)&#x7684;&#x540C;&#x6570;&#x91CF;&#x7EA7;&#x51FD;&#x6570;.&#x8BB0;&#x4F5C;T(n)=O(f(n)),&#x79F0;O(f(n)) &#x4E3A;&#x7B97;&#x6CD5;&#x7684;&#x6E10;&#x8FDB;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;,&#x7B80;&#x79F0;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;.</p>
</li>
<li><p>&#x5728;&#x5404;&#x79CD;&#x4E0D;&#x540C;&#x7B97;&#x6CD5;&#x4E2D;,&#x82E5;&#x7B97;&#x6CD5;&#x4E2D;&#x8BED;&#x53E5;&#x6267;&#x884C;&#x6B21;&#x6570;&#x4E3A;&#x4E00;&#x4E2A;&#x5E38;&#x6570;,&#x5219;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x4E3A;O(1),&#x53E6;&#x5916;,&#x5728;&#x65F6;&#x95F4;&#x9891;&#x5EA6;&#x4E0D;&#x76F8;&#x540C;&#x65F6;,&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x6709;&#x53EF;&#x80FD;&#x76F8;&#x540C;,&#x5982;T(n)=n2+3n+4&#x4E0E;T(n)=4n2+2n+1&#x5B83;&#x4EEC;&#x7684;&#x9891;&#x5EA6;&#x4E0D;&#x540C;,&#x4F46;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x76F8;&#x540C;,&#x90FD;&#x4E3A;O(n2).</p>
</li>
<li><p>&#x6309;&#x6570;&#x91CF;&#x7EA7;&#x9012;&#x589E;&#x6392;&#x5217;,&#x5E38;&#x89C1;&#x7684;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x6709;&#xFF1A;</p>
<p>  O(1)&#x79F0;&#x4E3A;&#x5E38;&#x91CF;&#x7EA7;&#xFF0C;&#x7B97;&#x6CD5;&#x7684;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x662F;&#x4E00;&#x4E2A;&#x5E38;&#x6570;&#x3002;</p>
<p>  O(n)&#x79F0;&#x4E3A;&#x7EBF;&#x6027;&#x7EA7;&#xFF0C;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x662F;&#x6570;&#x636E;&#x91CF;n&#x7684;&#x7EBF;&#x6027;&#x51FD;&#x6570;&#x3002;</p>
<p>  O(n&#xB2;)&#x79F0;&#x4E3A;&#x5E73;&#x65B9;&#x7EA7;&#xFF0C;&#x4E0E;&#x6570;&#x636E;&#x91CF;n&#x7684;&#x4E8C;&#x6B21;&#x591A;&#x9879;&#x5F0F;&#x51FD;&#x6570;&#x5C5E;&#x4E8E;&#x540C;&#x4E00;&#x6570;&#x91CF;&#x7EA7;&#x3002;</p>
<p>  O(n&#xB3;)&#x79F0;&#x4E3A;&#x7ACB;&#x65B9;&#x7EA7;&#xFF0C;&#x662F;n&#x7684;&#x4E09;&#x6B21;&#x591A;&#x9879;&#x5F0F;&#x51FD;&#x6570;&#x3002;</p>
<p>  O(logn)&#x79F0;&#x4E3A;&#x5BF9;&#x6570;&#x7EA7;&#xFF0C;&#x662F;n&#x7684;&#x5BF9;&#x6570;&#x51FD;&#x6570;&#x3002;</p>
<p>  O(nlogn)&#x79F0;&#x4E3A;&#x4ECB;&#x4E8E;&#x7EBF;&#x6027;&#x7EA7;&#x548C;&#x5E73;&#x65B9;&#x7EA7;&#x4E4B;&#x95F4;&#x7684;&#x4E00;&#x79CD;&#x6570;&#x91CF;&#x7EA7;</p>
<p>  O(2&#x207F;)&#x79F0;&#x4E3A;&#x6307;&#x6570;&#x7EA7;&#xFF0C;&#x4E0E;&#x6570;&#x636E;&#x91CF;n&#x7684;&#x6307;&#x6570;&#x51FD;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x91CF;&#x7EA7;&#x3002;</p>
<p>  O(n!)&#x79F0;&#x4E3A;&#x9636;&#x4E58;&#x7EA7;&#xFF0C;&#x4E0E;&#x6570;&#x636E;&#x91CF;n&#x7684;&#x9636;&#x4E58;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x91CF;&#x7EA7;&#x3002;</p>
<p>  &#x5B83;&#x4EEC;&#x4E4B;&#x95F4;&#x7684;&#x5173;&#x7CFB;&#x662F;&#xFF1A; O(1)&lt;O(logn)&lt;O(n)&lt;O(nlogn)&lt;O(n&#xB2;)&lt;O(n&#xB3;)&lt;O(2&#x207F;)&lt;O(n!)&#xFF0C;&#x968F;&#x7740;&#x95EE;&#x9898;&#x89C4;&#x6A21;n&#x7684;&#x4E0D;&#x65AD;&#x589E;&#x5927;,&#x4E0A;&#x8FF0;&#x65F6;&#x95F4;&#x590D;&#x6742;&#x5EA6;&#x4E0D;&#x65AD;&#x589E;&#x5927;,&#x7B97;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x6548;&#x7387;&#x8D8A;&#x4F4E;.</p>
</li>
</ul>
<h2 id="2&#x7A7A;&#x95F4;&#x590D;&#x6742;&#x5EA6;">2.&#x7A7A;&#x95F4;&#x590D;&#x6742;&#x5EA6;</h2>
<ul>
<li>&#x8BC4;&#x4F30;&#x6267;&#x884C;&#x7A0B;&#x5E8F;&#x6240;&#x9700;&#x7684;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;&#x3002;&#x53EF;&#x4EE5;&#x4F30;&#x7B97;&#x51FA;&#x7A0B;&#x5E8F;&#x5BF9;&#x8BA1;&#x7B97;&#x673A;&#x5185;&#x5B58;&#x7684;&#x4F7F;&#x7528;&#x7A0B;&#x5EA6;&#x3002;&#x4E0D;&#x5305;&#x62EC;&#x7B97;&#x6CD5;&#x7A0B;&#x5E8F;&#x4EE3;&#x7801;&#x548C;&#x6240;&#x5904;&#x7406;&#x7684;&#x6570;&#x636E;&#x672C;&#x8EAB;&#x6240;&#x5360;&#x7A7A;&#x95F4;&#x90E8;&#x5206;&#x3002;&#x901A;&#x5E38;&#x7528;&#x6240;&#x4F7F;&#x7528;&#x989D;&#x5916;&#x7A7A;&#x95F4;&#x7684;&#x5B57;&#x8282;&#x6570;&#x8868;&#x793A;&#x3002;&#x5176;&#x7B97;&#x6CD5;&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#xFF0C;&#x8BB0;&#x4E3A;S(n)=O(f(n))&#xFF0C;&#x5176;&#x4E2D;&#xFF0C;n&#x8868;&#x793A;&#x95EE;&#x9898;&#x89C4;&#x6A21;&#x3002;</li>
</ul>
<h2 id="3&#x5E38;&#x7528;&#x7684;&#x6392;&#x5E8F;&#x7B97;&#x6CD5;">3.&#x5E38;&#x7528;&#x7684;&#x6392;&#x5E8F;&#x7B97;&#x6CD5;</h2>
<ul>
<li><p>&#x9009;&#x62E9;&#x6392;&#x5E8F;&#x3001;&#x5192;&#x6CE1;&#x6392;&#x5E8F;&#x3001;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#x4E09;&#x79CD;&#x6392;&#x5E8F;&#x7B97;&#x6CD5;&#x53EF;&#x4EE5;&#x603B;&#x7ED3;&#x4E3A;&#x5982;&#x4E0B;&#xFF1A;</p>
<p>  &#x90FD;&#x5C06;&#x6570;&#x7EC4;&#x5206;&#x4E3A;&#x5DF2;&#x6392;&#x5E8F;&#x90E8;&#x5206;&#x548C;&#x672A;&#x6392;&#x5E8F;&#x90E8;&#x5206;&#x3002;</p>
<p>  &#x9009;&#x62E9;&#x6392;&#x5E8F;&#x5C06;&#x5DF2;&#x6392;&#x5E8F;&#x90E8;&#x5206;&#x5B9A;&#x4E49;&#x5728;&#x5DE6;&#x7AEF;&#xFF0C;&#x7136;&#x540E;&#x9009;&#x62E9;&#x672A;&#x6392;&#x5E8F;&#x90E8;&#x5206;&#x7684;&#x6700;&#x5C0F;&#x5143;&#x7D20;&#x548C;&#x672A;&#x6392;&#x5E8F;&#x90E8;&#x5206;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x4EA4;&#x6362;&#x3002;</p>
<p>  &#x5192;&#x6CE1;&#x6392;&#x5E8F;&#x5C06;&#x5DF2;&#x6392;&#x5E8F;&#x90E8;&#x5206;&#x5B9A;&#x4E49;&#x5728;&#x53F3;&#x7AEF;&#xFF0C;&#x5728;&#x904D;&#x5386;&#x672A;&#x6392;&#x5E8F;&#x90E8;&#x5206;&#x7684;&#x8FC7;&#x7A0B;&#x6267;&#x884C;&#x4EA4;&#x6362;&#xFF0C;&#x5C06;&#x6700;&#x5927;&#x5143;&#x7D20;&#x4EA4;&#x6362;&#x5230;&#x6700;&#x53F3;&#x7AEF;&#x3002;</p>
<p>  &#x63D2;&#x5165;&#x6392;&#x5E8F;&#x5C06;&#x5DF2;&#x6392;&#x5E8F;&#x90E8;&#x5206;&#x5B9A;&#x4E49;&#x5728;&#x5DE6;&#x7AEF;&#xFF0C;&#x5C06;&#x672A;&#x6392;&#x5E8F;&#x90E8;&#x5206;&#x5143;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x63D2;&#x5165;&#x5230;&#x5DF2;&#x6392;&#x5E8F;&#x90E8;&#x5206;&#x5408;&#x9002;&#x7684;&#x4F4D;&#x7F6E;&#x3002;</p>
</li>
</ul>
<pre><code class="lang-c"><span class="hljs-comment">/** 
 *    &#x3010;&#x9009;&#x62E9;&#x6392;&#x5E8F;&#x3011;&#xFF1A;&#x6700;&#x503C;&#x51FA;&#x73B0;&#x5728;&#x8D77;&#x59CB;&#x7AEF;
 *    
 *    &#x7B2C;1&#x8D9F;&#xFF1A;&#x5728;n&#x4E2A;&#x6570;&#x4E2D;&#x627E;&#x5230;&#x6700;&#x5C0F;(&#x5927;)&#x6570;&#x4E0E;&#x7B2C;&#x4E00;&#x4E2A;&#x6570;&#x4EA4;&#x6362;&#x4F4D;&#x7F6E;
 *    &#x7B2C;2&#x8D9F;&#xFF1A;&#x5728;&#x5269;&#x4E0B;n-1&#x4E2A;&#x6570;&#x4E2D;&#x627E;&#x5230;&#x6700;&#x5C0F;(&#x5927;)&#x6570;&#x4E0E;&#x7B2C;&#x4E8C;&#x4E2A;&#x6570;&#x4EA4;&#x6362;&#x4F4D;&#x7F6E;
 *    &#x91CD;&#x590D;&#x8FD9;&#x6837;&#x7684;&#x64CD;&#x4F5C;...&#x4F9D;&#x6B21;&#x4E0E;&#x7B2C;&#x4E09;&#x4E2A;&#x3001;&#x7B2C;&#x56DB;&#x4E2A;...&#x6570;&#x4EA4;&#x6362;&#x4F4D;&#x7F6E;
 *    &#x7B2C;n-1&#x8D9F;&#xFF0C;&#x6700;&#x7EC8;&#x53EF;&#x5B9E;&#x73B0;&#x6570;&#x636E;&#x7684;&#x5347;&#x5E8F;&#xFF08;&#x964D;&#x5E8F;&#xFF09;&#x6392;&#x5217;&#x3002;
 *
 */</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">selectSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *arr, <span class="hljs-keyword">int</span> length)</span> </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; length - <span class="hljs-number">1</span>; i++) { <span class="hljs-comment">//&#x8D9F;&#x6570;</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i + <span class="hljs-number">1</span>; j &lt; length; j++) { <span class="hljs-comment">//&#x6BD4;&#x8F83;&#x6B21;&#x6570;</span>
            <span class="hljs-keyword">if</span> (arr[i] &gt; arr[j]) {
                <span class="hljs-keyword">int</span> temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

<span class="hljs-comment">/** 
 *    &#x3010;&#x5192;&#x6CE1;&#x6392;&#x5E8F;&#x3011;&#xFF1A;&#x76F8;&#x90BB;&#x5143;&#x7D20;&#x4E24;&#x4E24;&#x6BD4;&#x8F83;&#xFF0C;&#x6BD4;&#x8F83;&#x5B8C;&#x4E00;&#x8D9F;&#xFF0C;&#x6700;&#x503C;&#x51FA;&#x73B0;&#x5728;&#x672B;&#x5C3E;
 *    &#x7B2C;1&#x8D9F;&#xFF1A;&#x4F9D;&#x6B21;&#x6BD4;&#x8F83;&#x76F8;&#x90BB;&#x7684;&#x4E24;&#x4E2A;&#x6570;&#xFF0C;&#x4E0D;&#x65AD;&#x4EA4;&#x6362;&#xFF08;&#x5C0F;&#x6570;&#x653E;&#x524D;&#xFF0C;&#x5927;&#x6570;&#x653E;&#x540E;&#xFF09;&#x9010;&#x4E2A;&#x63A8;&#x8FDB;&#xFF0C;&#x6700;&#x503C;&#x6700;&#x540E;&#x51FA;&#x73B0;&#x5728;&#x7B2C;n&#x4E2A;&#x5143;&#x7D20;&#x4F4D;&#x7F6E;
 *    &#x7B2C;2&#x8D9F;&#xFF1A;&#x4F9D;&#x6B21;&#x6BD4;&#x8F83;&#x76F8;&#x90BB;&#x7684;&#x4E24;&#x4E2A;&#x6570;&#xFF0C;&#x4E0D;&#x65AD;&#x4EA4;&#x6362;&#xFF08;&#x5C0F;&#x6570;&#x653E;&#x524D;&#xFF0C;&#x5927;&#x6570;&#x653E;&#x540E;&#xFF09;&#x9010;&#x4E2A;&#x63A8;&#x8FDB;&#xFF0C;&#x6700;&#x503C;&#x6700;&#x540E;&#x51FA;&#x73B0;&#x5728;&#x7B2C;n-1&#x4E2A;&#x5143;&#x7D20;&#x4F4D;&#x7F6E;
 *     &#x2026;&#x2026;   &#x2026;&#x2026;
 *    &#x7B2C;n-1&#x8D9F;&#xFF1A;&#x4F9D;&#x6B21;&#x6BD4;&#x8F83;&#x76F8;&#x90BB;&#x7684;&#x4E24;&#x4E2A;&#x6570;&#xFF0C;&#x4E0D;&#x65AD;&#x4EA4;&#x6362;&#xFF08;&#x5C0F;&#x6570;&#x653E;&#x524D;&#xFF0C;&#x5927;&#x6570;&#x653E;&#x540E;&#xFF09;&#x9010;&#x4E2A;&#x63A8;&#x8FDB;&#xFF0C;&#x6700;&#x503C;&#x6700;&#x540E;&#x51FA;&#x73B0;&#x5728;&#x7B2C;2&#x4E2A;&#x5143;&#x7D20;&#x4F4D;&#x7F6E;    
 */</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">bublleSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *arr, <span class="hljs-keyword">int</span> length)</span> </span>{
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; length - <span class="hljs-number">1</span>; i++) { <span class="hljs-comment">//&#x8D9F;&#x6570;</span>
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j &lt; length - i - <span class="hljs-number">1</span>; j++) { <span class="hljs-comment">//&#x6BD4;&#x8F83;&#x6B21;&#x6570;</span>
            <span class="hljs-keyword">if</span>(arr[j] &gt; arr[j+<span class="hljs-number">1</span>]) {
                <span class="hljs-keyword">int</span> temp = arr[j];
                arr[j] = arr[j+<span class="hljs-number">1</span>];
                arr[j+<span class="hljs-number">1</span>] = temp;
            }
        } 
    }
}

<span class="hljs-comment">/**
 *    &#x6298;&#x534A;&#x67E5;&#x627E;&#xFF1A;&#x4F18;&#x5316;&#x67E5;&#x627E;&#x65F6;&#x95F4;&#xFF08;&#x4E0D;&#x7528;&#x904D;&#x5386;&#x5168;&#x90E8;&#x6570;&#x636E;&#xFF09;
 *
 *    &#x6298;&#x534A;&#x67E5;&#x627E;&#x7684;&#x539F;&#x7406;&#xFF1A;
 *   1&gt; &#x6570;&#x7EC4;&#x5FC5;&#x987B;&#x662F;&#x6709;&#x5E8F;&#x7684;
 *   2&gt; &#x5FC5;&#x987B;&#x5DF2;&#x77E5;min&#x548C;max&#xFF08;&#x77E5;&#x9053;&#x8303;&#x56F4;&#xFF09;
 *   3&gt; &#x52A8;&#x6001;&#x8BA1;&#x7B97;mid&#x7684;&#x503C;&#xFF0C;&#x53D6;&#x51FA;mid&#x5BF9;&#x5E94;&#x7684;&#x503C;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;
 *   4&gt; &#x5982;&#x679C;mid&#x5BF9;&#x5E94;&#x7684;&#x503C;&#x5927;&#x4E8E;&#x8981;&#x67E5;&#x627E;&#x7684;&#x503C;&#xFF0C;&#x90A3;&#x4E48;max&#x8981;&#x53D8;&#x5C0F;&#x4E3A;mid-1
 *   5&gt; &#x5982;&#x679C;mid&#x5BF9;&#x5E94;&#x7684;&#x503C;&#x5C0F;&#x4E8E;&#x8981;&#x67E5;&#x627E;&#x7684;&#x503C;&#xFF0C;&#x90A3;&#x4E48;min&#x8981;&#x53D8;&#x5927;&#x4E3A;mid+1
 *
 */</span> 

<span class="hljs-comment">// &#x5DF2;&#x77E5;&#x4E00;&#x4E2A;&#x6709;&#x5E8F;&#x6570;&#x7EC4;, &#x548C;&#x4E00;&#x4E2A;key, &#x8981;&#x6C42;&#x4ECE;&#x6570;&#x7EC4;&#x4E2D;&#x627E;&#x5230;key&#x5BF9;&#x5E94;&#x7684;&#x7D22;&#x5F15;&#x4F4D;&#x7F6E; </span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">findKey</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *arr, <span class="hljs-keyword">int</span> length, <span class="hljs-keyword">int</span> key)</span> </span>{
    <span class="hljs-keyword">int</span> min = <span class="hljs-number">0</span>, max = length - <span class="hljs-number">1</span>, mid;
    <span class="hljs-keyword">while</span> (min &lt;= max) {
        mid = (min + max) / <span class="hljs-number">2</span>; <span class="hljs-comment">//&#x8BA1;&#x7B97;&#x4E2D;&#x95F4;&#x503C;</span>
        <span class="hljs-keyword">if</span> (key &gt; arr[mid]) {
            min = mid + <span class="hljs-number">1</span>;
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (key &lt; arr[mid]) {
            max = mid - <span class="hljs-number">1</span>;
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">return</span> mid;
        }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
}
</code></pre>
<h2 id="4&#x5B57;&#x7B26;&#x4E32;&#x53CD;&#x8F6C;">4.&#x5B57;&#x7B26;&#x4E32;&#x53CD;&#x8F6C;</h2>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">char_reverse</span> <span class="hljs-params">(<span class="hljs-keyword">char</span> *cha)</span> </span>{

    <span class="hljs-comment">// &#x5B9A;&#x4E49;&#x5934;&#x90E8;&#x6307;&#x9488;</span>
    <span class="hljs-keyword">char</span> *begin = cha;
    <span class="hljs-comment">// &#x5B9A;&#x4E49;&#x5C3E;&#x90E8;&#x6307;&#x9488;</span>
    <span class="hljs-keyword">char</span> *end = cha + <span class="hljs-built_in">strlen</span>(cha) <span class="hljs-number">-1</span>;


    <span class="hljs-keyword">while</span> (begin &lt; end) {

        <span class="hljs-keyword">char</span> temp = *begin;
        *(begin++) = *end;
        *(end--) = temp;
    }
}
</code></pre>
<h2 id="5&#x94FE;&#x8868;&#x53CD;&#x8F6C;&#xFF08;&#x5934;&#x5DEE;&#x6CD5;&#xFF09;">5.&#x94FE;&#x8868;&#x53CD;&#x8F6C;&#xFF08;&#x5934;&#x5DEE;&#x6CD5;&#xFF09;</h2>
<p>.h&#x58F0;&#x660E;&#x6587;&#x4EF6;</p>
<pre><code class="lang-c"><span class="hljs-meta">#import &lt;Foundation/Foundation.h&gt;</span>

<span class="hljs-comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x94FE;&#x8868;</span>
<span class="hljs-keyword">struct</span> Node {
    <span class="hljs-keyword">int</span> data;
    <span class="hljs-keyword">struct</span> Node *next;
};

@interface ReverseList : NSObject

<span class="hljs-comment">// &#x94FE;&#x8868;&#x53CD;&#x8F6C;</span>
<span class="hljs-function"><span class="hljs-keyword">struct</span> Node* <span class="hljs-title">reverseList</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> Node *head)</span></span>;

<span class="hljs-comment">// &#x6784;&#x9020;&#x4E00;&#x4E2A;&#x94FE;&#x8868;</span>
<span class="hljs-function"><span class="hljs-keyword">struct</span> Node* <span class="hljs-title">constructList</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span>;

<span class="hljs-comment">// &#x6253;&#x5370;&#x94FE;&#x8868;&#x4E2D;&#x7684;&#x6570;&#x636E;</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> Node *head)</span></span>;

@end
</code></pre>
<p>.m&#x5B9E;&#x73B0;&#x6587;&#x4EF6;</p>
<pre><code class="lang-c"><span class="hljs-meta">#import <span class="hljs-string">&quot;ReverseList.h&quot;</span></span>

@<span class="hljs-function">implementation ReverseList

<span class="hljs-keyword">struct</span> Node* <span class="hljs-title">reverseList</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> Node *head)</span>
</span>{
    <span class="hljs-comment">// &#x5B9A;&#x4E49;&#x904D;&#x5386;&#x6307;&#x9488;&#xFF0C;&#x521D;&#x59CB;&#x5316;&#x4E3A;&#x5934;&#x7ED3;&#x70B9;</span>
    <span class="hljs-keyword">struct</span> Node *p = head;

    <span class="hljs-comment">// &#x53CD;&#x8F6C;&#x540E;&#x7684;&#x94FE;&#x8868;&#x5934;&#x90E8;</span>
    <span class="hljs-keyword">struct</span> Node *newH = <span class="hljs-literal">NULL</span>;

    <span class="hljs-comment">// &#x904D;&#x5386;&#x94FE;&#x8868;</span>
    <span class="hljs-keyword">while</span> (p != <span class="hljs-literal">NULL</span>) {

        <span class="hljs-comment">// &#x8BB0;&#x5F55;&#x4E0B;&#x4E00;&#x4E2A;&#x7ED3;&#x70B9;</span>
        <span class="hljs-keyword">struct</span> Node *temp = p-&gt;next;
        <span class="hljs-comment">// &#x5F53;&#x524D;&#x7ED3;&#x70B9;&#x7684;next&#x6307;&#x5411;&#x65B0;&#x94FE;&#x8868;&#x5934;&#x90E8;</span>
        p-&gt;next = newH;
        <span class="hljs-comment">// &#x66F4;&#x6539;&#x65B0;&#x94FE;&#x8868;&#x5934;&#x90E8;&#x4E3A;&#x5F53;&#x524D;&#x7ED3;&#x70B9;</span>
        newH = p;
        <span class="hljs-comment">// &#x79FB;&#x52A8;p&#x6307;&#x9488;</span>
        p = temp;
    }

    <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x53CD;&#x8F6C;&#x540E;&#x7684;&#x94FE;&#x8868;&#x5934;&#x7ED3;&#x70B9;</span>
    <span class="hljs-keyword">return</span> newH;
}

<span class="hljs-function"><span class="hljs-keyword">struct</span> Node* <span class="hljs-title">constructList</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span>
</span>{
    <span class="hljs-comment">// &#x5934;&#x7ED3;&#x70B9;&#x5B9A;&#x4E49;</span>
    <span class="hljs-keyword">struct</span> Node *head = <span class="hljs-literal">NULL</span>;
    <span class="hljs-comment">// &#x8BB0;&#x5F55;&#x5F53;&#x524D;&#x5C3E;&#x7ED3;&#x70B9;</span>
    <span class="hljs-keyword">struct</span> Node *cur = <span class="hljs-literal">NULL</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">5</span>; i++) {
        <span class="hljs-keyword">struct</span> Node *node = <span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">struct</span> Node));
        node-&gt;data = i;

        <span class="hljs-comment">// &#x5934;&#x7ED3;&#x70B9;&#x4E3A;&#x7A7A;&#xFF0C;&#x65B0;&#x7ED3;&#x70B9;&#x5373;&#x4E3A;&#x5934;&#x7ED3;&#x70B9;</span>
        <span class="hljs-keyword">if</span> (head == <span class="hljs-literal">NULL</span>) {
            head = node;
        }
        <span class="hljs-comment">// &#x5F53;&#x524D;&#x7ED3;&#x70B9;&#x7684;next&#x4E3A;&#x65B0;&#x7ED3;&#x70B9;</span>
        <span class="hljs-keyword">else</span>{
            cur-&gt;next = node;
        }

        <span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x5F53;&#x524D;&#x7ED3;&#x70B9;&#x4E3A;&#x65B0;&#x7ED3;&#x70B9;</span>
        cur = node;
    }

    <span class="hljs-keyword">return</span> head;
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> Node *head)</span>
</span>{
    <span class="hljs-keyword">struct</span> Node* temp = head;
    <span class="hljs-keyword">while</span> (temp != <span class="hljs-literal">NULL</span>) {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;node is %d \n&quot;</span>, temp-&gt;data);
        temp = temp-&gt;next;
    }
}

@end
</code></pre>
<h2 id="6&#x6709;&#x5E8F;&#x6570;&#x7EC4;&#x5408;&#x5E76;">6.&#x6709;&#x5E8F;&#x6570;&#x7EC4;&#x5408;&#x5E76;</h2>
<p>.h&#x58F0;&#x660E;&#x6587;&#x4EF6;</p>
<pre><code class="lang-c"><span class="hljs-meta">#import &lt;Foundation/Foundation.h&gt;</span>

@interface MergeSortedList : NSObject
<span class="hljs-comment">// &#x5C06;&#x6709;&#x5E8F;&#x6570;&#x7EC4;a&#x548C;b&#x7684;&#x503C;&#x5408;&#x5E76;&#x5230;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;result&#x5F53;&#x4E2D;&#xFF0C;&#x4E14;&#x4ECD;&#x7136;&#x4FDD;&#x6301;&#x6709;&#x5E8F;</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">mergeList</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a[], <span class="hljs-keyword">int</span> aLen, <span class="hljs-keyword">int</span> b[], <span class="hljs-keyword">int</span> bLen, <span class="hljs-keyword">int</span> result[])</span></span>;

@end
</code></pre>
<p>.m&#x5B9E;&#x73B0;&#x6587;&#x4EF6;</p>
<pre><code class="lang-c"><span class="hljs-meta">#import <span class="hljs-string">&quot;MergeSortedList.h&quot;</span></span>

@<span class="hljs-function">implementation MergeSortedList

<span class="hljs-keyword">void</span> <span class="hljs-title">mergeList</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a[], <span class="hljs-keyword">int</span> aLen, <span class="hljs-keyword">int</span> b[], <span class="hljs-keyword">int</span> bLen, <span class="hljs-keyword">int</span> result[])</span>
</span>{
    <span class="hljs-keyword">int</span> p = <span class="hljs-number">0</span>; <span class="hljs-comment">// &#x904D;&#x5386;&#x6570;&#x7EC4;a&#x7684;&#x6307;&#x9488;</span>
    <span class="hljs-keyword">int</span> q = <span class="hljs-number">0</span>; <span class="hljs-comment">// &#x904D;&#x5386;&#x6570;&#x7EC4;b&#x7684;&#x6307;&#x9488;</span>
    <span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; <span class="hljs-comment">// &#x8BB0;&#x5F55;&#x5F53;&#x524D;&#x5B58;&#x50A8;&#x4F4D;&#x7F6E;</span>

    <span class="hljs-comment">// &#x4EFB;&#x4E00;&#x6570;&#x7EC4;&#x6CA1;&#x6709;&#x5230;&#x8FBE;&#x8FB9;&#x754C;&#x5219;&#x8FDB;&#x884C;&#x904D;&#x5386;</span>
    <span class="hljs-keyword">while</span> (p &lt; aLen &amp;&amp; q &lt; bLen) {
        <span class="hljs-comment">// &#x5982;&#x679C;a&#x6570;&#x7EC4;&#x5BF9;&#x5E94;&#x4F4D;&#x7F6E;&#x7684;&#x503C;&#x5C0F;&#x4E8E;b&#x6570;&#x7EC4;&#x5BF9;&#x5E94;&#x4F4D;&#x7F6E;&#x7684;&#x503C;</span>
        <span class="hljs-keyword">if</span> (a[p] &lt;= b[q]) {
            <span class="hljs-comment">// &#x5B58;&#x50A8;a&#x6570;&#x7EC4;&#x7684;&#x503C;</span>
            result[i] = a[p];
            <span class="hljs-comment">// &#x79FB;&#x52A8;a&#x6570;&#x7EC4;&#x7684;&#x904D;&#x5386;&#x6307;&#x9488;</span>
            p++;
        }
        <span class="hljs-keyword">else</span>{
            <span class="hljs-comment">// &#x5B58;&#x50A8;b&#x6570;&#x7EC4;&#x7684;&#x503C;</span>
            result[i] = b[q];
            <span class="hljs-comment">// &#x79FB;&#x52A8;b&#x6570;&#x7EC4;&#x7684;&#x904D;&#x5386;&#x6307;&#x9488;</span>
            q++;
        }
        <span class="hljs-comment">// &#x6307;&#x5411;&#x5408;&#x5E76;&#x7ED3;&#x679C;&#x7684;&#x4E0B;&#x4E00;&#x4E2A;&#x5B58;&#x50A8;&#x4F4D;&#x7F6E;</span>
        i++;
    }

    <span class="hljs-comment">// &#x5982;&#x679C;a&#x6570;&#x7EC4;&#x6709;&#x5269;&#x4F59;</span>
    <span class="hljs-keyword">while</span> (p &lt; aLen) {
        <span class="hljs-comment">// &#x5C06;a&#x6570;&#x7EC4;&#x5269;&#x4F59;&#x90E8;&#x5206;&#x62FC;&#x63A5;&#x5230;&#x5408;&#x5E76;&#x7ED3;&#x679C;&#x7684;&#x540E;&#x9762;</span>
        result[i] = a[p++];
        i++;
    }

    <span class="hljs-comment">// &#x5982;&#x679C;b&#x6570;&#x7EC4;&#x6709;&#x5269;&#x4F59;</span>
    <span class="hljs-keyword">while</span> (q &lt; bLen) {
        <span class="hljs-comment">// &#x5C06;b&#x6570;&#x7EC4;&#x5269;&#x4F59;&#x90E8;&#x5206;&#x62FC;&#x63A5;&#x5230;&#x5408;&#x5E76;&#x7ED3;&#x679C;&#x7684;&#x540E;&#x9762;</span>
        result[i] = b[q++];
        i++;
    }
}

@end
</code></pre>
<h2 id="7&#x67E5;&#x627E;&#x7B2C;&#x4E00;&#x4E2A;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x7684;&#x5B57;&#x7B26;&#xFF08;hash&#x67E5;&#x627E;&#xFF09;">7.&#x67E5;&#x627E;&#x7B2C;&#x4E00;&#x4E2A;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x7684;&#x5B57;&#x7B26;&#xFF08;Hash&#x67E5;&#x627E;&#xFF09;</h2>
<p>.h&#x58F0;&#x660E;&#x6587;&#x4EF6;</p>
<pre><code class="lang-c"><span class="hljs-meta">#import &lt;Foundation/Foundation.h&gt;</span>

@interface HashFind : NSObject

<span class="hljs-comment">// &#x67E5;&#x627E;&#x7B2C;&#x4E00;&#x4E2A;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x7684;&#x5B57;&#x7B26;</span>
<span class="hljs-function"><span class="hljs-keyword">char</span> <span class="hljs-title">findFirstChar</span><span class="hljs-params">(<span class="hljs-keyword">char</span>* cha)</span></span>;

@end
</code></pre>
<p>.m&#x5B9E;&#x73B0;&#x6587;&#x4EF6;</p>
<pre><code class="lang-c"><span class="hljs-meta">#import <span class="hljs-string">&quot;HashFind.h&quot;</span></span>

@<span class="hljs-function">implementation HashFind

<span class="hljs-keyword">char</span> <span class="hljs-title">findFirstChar</span><span class="hljs-params">(<span class="hljs-keyword">char</span>* cha)</span>
</span>{
    <span class="hljs-keyword">char</span> result = <span class="hljs-string">&apos;\0&apos;</span>;

    <span class="hljs-comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6570;&#x7EC4; &#x7528;&#x6765;&#x5B58;&#x50A8;&#x5404;&#x4E2A;&#x5B57;&#x6BCD;&#x51FA;&#x73B0;&#x6B21;&#x6570;</span>
    <span class="hljs-keyword">int</span> <span class="hljs-built_in">array</span>[<span class="hljs-number">256</span>];

    <span class="hljs-comment">// &#x5BF9;&#x6570;&#x7EC4;&#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;&#x64CD;&#x4F5C;</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">256</span>; i++) {
        <span class="hljs-built_in">array</span>[i] =<span class="hljs-number">0</span>;
    }
    <span class="hljs-comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6307;&#x9488; &#x6307;&#x5411;&#x5F53;&#x524D;&#x5B57;&#x7B26;&#x4E32;&#x5934;&#x90E8;</span>
    <span class="hljs-keyword">char</span>* p = cha;
    <span class="hljs-comment">// &#x904D;&#x5386;&#x6BCF;&#x4E2A;&#x5B57;&#x7B26;</span>
    <span class="hljs-keyword">while</span> (*p != <span class="hljs-string">&apos;\0&apos;</span>) {
        <span class="hljs-comment">// &#x5728;&#x5B57;&#x6BCD;&#x5BF9;&#x5E94;&#x5B58;&#x50A8;&#x4F4D;&#x7F6E; &#x8FDB;&#x884C;&#x51FA;&#x73B0;&#x6B21;&#x6570;+1&#x64CD;&#x4F5C;</span>
        <span class="hljs-built_in">array</span>[*(p++)]++;
    }

    <span class="hljs-comment">// &#x5C06;P&#x6307;&#x9488;&#x91CD;&#x65B0;&#x6307;&#x5411;&#x5B57;&#x7B26;&#x4E32;&#x5934;&#x90E8;</span>
    p = cha;
    <span class="hljs-comment">// &#x904D;&#x5386;&#x6BCF;&#x4E2A;&#x5B57;&#x6BCD;&#x7684;&#x51FA;&#x73B0;&#x6B21;&#x6570;</span>
    <span class="hljs-keyword">while</span> (*p != <span class="hljs-string">&apos;\0&apos;</span>) {
        <span class="hljs-comment">// &#x9047;&#x5230;&#x7B2C;&#x4E00;&#x4E2A;&#x51FA;&#x73B0;&#x6B21;&#x6570;&#x4E3A;1&#x7684;&#x5B57;&#x7B26;&#xFF0C;&#x6253;&#x5370;&#x7ED3;&#x679C;</span>
        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">array</span>[*p] == <span class="hljs-number">1</span>)
        {
            result = *p;
            <span class="hljs-keyword">break</span>;
        }
        <span class="hljs-comment">// &#x53CD;&#x4E4B;&#x7EE7;&#x7EED;&#x5411;&#x540E;&#x904D;&#x5386;</span>
        p++;
    }

    <span class="hljs-keyword">return</span> result;
}

@end
</code></pre>
<h2 id="8&#x67E5;&#x627E;&#x4E24;&#x4E2A;&#x5B50;&#x89C6;&#x56FE;&#x7684;&#x5171;&#x540C;&#x7236;&#x89C6;&#x56FE;">8.&#x67E5;&#x627E;&#x4E24;&#x4E2A;&#x5B50;&#x89C6;&#x56FE;&#x7684;&#x5171;&#x540C;&#x7236;&#x89C6;&#x56FE;</h2>
<p>.h&#x58F0;&#x660E;&#x6587;&#x4EF6;</p>
<pre><code class="lang-c"><span class="hljs-meta">#import &lt;Foundation/Foundation.h&gt;</span>
<span class="hljs-meta">#import &lt;UIKit/UIKit.h&gt;</span>
@interface CommonSuperFind : NSObject

<span class="hljs-comment">// &#x67E5;&#x627E;&#x4E24;&#x4E2A;&#x89C6;&#x56FE;&#x7684;&#x5171;&#x540C;&#x7236;&#x89C6;&#x56FE;</span>
- (NSArray&lt;UIView *&gt; *)findCommonSuperView:(UIView *)view other:(UIView *)viewOther;

@end
</code></pre>
<p>.m&#x5B9E;&#x73B0;&#x6587;&#x4EF6;</p>
<pre><code class="lang-c"><span class="hljs-meta">#import <span class="hljs-string">&quot;CommonSuperFind.h&quot;</span></span>

@implementation CommonSuperFind

- (NSArray &lt;UIView *&gt; *)findCommonSuperView:(UIView *)viewOne other:(UIView *)viewOther
{
    NSMutableArray *result = [NSMutableArray <span class="hljs-built_in">array</span>];

    <span class="hljs-comment">// &#x67E5;&#x627E;&#x7B2C;&#x4E00;&#x4E2A;&#x89C6;&#x56FE;&#x7684;&#x6240;&#x6709;&#x7236;&#x89C6;&#x56FE;</span>
    NSArray *arrayOne = [self findSuperViews:viewOne];
    <span class="hljs-comment">// &#x67E5;&#x627E;&#x7B2C;&#x4E8C;&#x4E2A;&#x89C6;&#x56FE;&#x7684;&#x6240;&#x6709;&#x7236;&#x89C6;&#x56FE;</span>
    NSArray *arrayOther = [self findSuperViews:viewOther];

    <span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;
    <span class="hljs-comment">// &#x8D8A;&#x754C;&#x9650;&#x5236;&#x6761;&#x4EF6;</span>
    <span class="hljs-keyword">while</span> (i &lt; MIN((<span class="hljs-keyword">int</span>)arrayOne.count, (<span class="hljs-keyword">int</span>)arrayOther.count)) {
        <span class="hljs-comment">// &#x5012;&#x5E8F;&#x65B9;&#x5F0F;&#x83B7;&#x53D6;&#x5404;&#x4E2A;&#x89C6;&#x56FE;&#x7684;&#x7236;&#x89C6;&#x56FE;</span>
        UIView *superOne = [arrayOne objectAtIndex:arrayOne.count - i - <span class="hljs-number">1</span>];
        UIView *superOther = [arrayOther objectAtIndex:arrayOther.count - i - <span class="hljs-number">1</span>];

        <span class="hljs-comment">// &#x6BD4;&#x8F83;&#x5982;&#x679C;&#x76F8;&#x7B49; &#x5219;&#x4E3A;&#x5171;&#x540C;&#x7236;&#x89C6;&#x56FE;</span>
        <span class="hljs-keyword">if</span> (superOne == superOther) {
            [result addObject:superOne];
            i++;
        }
        <span class="hljs-comment">// &#x5982;&#x679C;&#x4E0D;&#x76F8;&#x7B49;&#xFF0C;&#x5219;&#x7ED3;&#x675F;&#x904D;&#x5386;</span>
        <span class="hljs-keyword">else</span>{
            <span class="hljs-keyword">break</span>;
        }
    }

    <span class="hljs-keyword">return</span> result;
}

- (NSArray &lt;UIView *&gt; *)findSuperViews:(UIView *)view
{
    <span class="hljs-comment">// &#x521D;&#x59CB;&#x5316;&#x4E3A;&#x7B2C;&#x4E00;&#x7236;&#x89C6;&#x56FE;</span>
    UIView *temp = view.superview;
    <span class="hljs-comment">// &#x4FDD;&#x5B58;&#x7ED3;&#x679C;&#x7684;&#x6570;&#x7EC4;</span>
    NSMutableArray *result = [NSMutableArray <span class="hljs-built_in">array</span>];
    <span class="hljs-keyword">while</span> (temp) {
        [result addObject:temp];
        <span class="hljs-comment">// &#x987A;&#x7740;superview&#x6307;&#x9488;&#x4E00;&#x76F4;&#x5411;&#x4E0A;&#x67E5;&#x627E;</span>
        temp = temp.superview;
    }
    <span class="hljs-keyword">return</span> result;
}

@end
</code></pre>
<h2 id="9&#x65E0;&#x5E8F;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x4E2D;&#x4F4D;&#x6570;&#x5FEB;&#x6392;&#x601D;&#x60F3;">9.&#x65E0;&#x5E8F;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x4E2D;&#x4F4D;&#x6570;(&#x5FEB;&#x6392;&#x601D;&#x60F3;)</h2>
<p>.h&#x58F0;&#x660E;&#x6587;&#x4EF6;</p>
<pre><code class="lang-c"><span class="hljs-meta">#import &lt;Foundation/Foundation.h&gt;</span>

@interface MedianFind : NSObject

<span class="hljs-comment">// &#x65E0;&#x5E8F;&#x6570;&#x7EC4;&#x4E2D;&#x4F4D;&#x6570;&#x67E5;&#x627E;</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">findMedian</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a[], <span class="hljs-keyword">int</span> aLen)</span></span>;

@end
</code></pre>
<pre><code class="lang-c">.m&#x5B9E;&#x73B0;&#x6587;&#x4EF6;
<span class="hljs-meta">#import <span class="hljs-string">&quot;MedianFind.h&quot;</span></span>

@implementation MedianFind

<span class="hljs-comment">//&#x6C42;&#x4E00;&#x4E2A;&#x65E0;&#x5E8F;&#x6570;&#x7EC4;&#x7684;&#x4E2D;&#x4F4D;&#x6570;</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">findMedian</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a[], <span class="hljs-keyword">int</span> aLen)</span>
</span>{
    <span class="hljs-keyword">int</span> low = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">int</span> high = aLen - <span class="hljs-number">1</span>;

    <span class="hljs-keyword">int</span> mid = (aLen - <span class="hljs-number">1</span>) / <span class="hljs-number">2</span>;
    <span class="hljs-keyword">int</span> div = PartSort(a, low, high);

    <span class="hljs-keyword">while</span> (div != mid)
    {
        <span class="hljs-keyword">if</span> (mid &lt; div)
        {
            <span class="hljs-comment">//&#x5DE6;&#x534A;&#x533A;&#x95F4;&#x627E;</span>
            div = PartSort(a, low, div - <span class="hljs-number">1</span>);
        }
        <span class="hljs-keyword">else</span>
        {
            <span class="hljs-comment">//&#x53F3;&#x534A;&#x533A;&#x95F4;&#x627E;</span>
            div = PartSort(a, div + <span class="hljs-number">1</span>, high);
        }
    }
    <span class="hljs-comment">//&#x627E;&#x5230;&#x4E86;</span>
    <span class="hljs-keyword">return</span> a[mid];
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">PartSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a[], <span class="hljs-keyword">int</span> start, <span class="hljs-keyword">int</span> end)</span>
</span>{
    <span class="hljs-keyword">int</span> low = start;
    <span class="hljs-keyword">int</span> high = end;

    <span class="hljs-comment">//&#x9009;&#x53D6;&#x5173;&#x952E;&#x5B57;</span>
    <span class="hljs-keyword">int</span> key = a[end];

    <span class="hljs-keyword">while</span> (low &lt; high)
    {
        <span class="hljs-comment">//&#x5DE6;&#x8FB9;&#x627E;&#x6BD4;key&#x5927;&#x7684;&#x503C;</span>
        <span class="hljs-keyword">while</span> (low &lt; high &amp;&amp; a[low] &lt;= key)
        {
            ++low;
        }

        <span class="hljs-comment">//&#x53F3;&#x8FB9;&#x627E;&#x6BD4;key&#x5C0F;&#x7684;&#x503C;</span>
        <span class="hljs-keyword">while</span> (low &lt; high &amp;&amp; a[high] &gt;= key)
        {
            --high;
        }

        <span class="hljs-keyword">if</span> (low &lt; high)
        {
            <span class="hljs-comment">//&#x627E;&#x5230;&#x4E4B;&#x540E;&#x4EA4;&#x6362;&#x5DE6;&#x53F3;&#x7684;&#x503C;</span>
            <span class="hljs-keyword">int</span> temp = a[low];
            a[low] = a[high];
            a[high] = temp;
        }
    }

    <span class="hljs-keyword">int</span> temp = a[high];
    a[high] = a[end];
    a[end] = temp;

    <span class="hljs-keyword">return</span> low;
}

@end
</code></pre>
<h2 id="10&#x7ED9;&#x5B9A;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#x6570;&#x7EC4;&#x548C;&#x4E00;&#x4E2A;&#x76EE;&#x6807;&#x503C;&#xFF0C;&#x627E;&#x51FA;&#x6570;&#x7EC4;&#x4E2D;&#x548C;&#x4E3A;&#x76EE;&#x6807;&#x503C;&#x7684;&#x4E24;&#x4E2A;&#x6570;&#x3002;">10.&#x7ED9;&#x5B9A;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#x6570;&#x7EC4;&#x548C;&#x4E00;&#x4E2A;&#x76EE;&#x6807;&#x503C;&#xFF0C;&#x627E;&#x51FA;&#x6570;&#x7EC4;&#x4E2D;&#x548C;&#x4E3A;&#x76EE;&#x6807;&#x503C;&#x7684;&#x4E24;&#x4E2A;&#x6570;&#x3002;</h2>
<pre><code class="lang-c">- (<span class="hljs-keyword">void</span>)viewDidLoad {

    [super viewDidLoad];

    NSArray *oriArray = @[@(<span class="hljs-number">2</span>),@(<span class="hljs-number">3</span>),@(<span class="hljs-number">6</span>),@(<span class="hljs-number">7</span>),@(<span class="hljs-number">22</span>),@(<span class="hljs-number">12</span>)];

    BOOL isHaveNums =  [self twoNumSumWithTarget:<span class="hljs-number">9</span> Array:oriArray];

    NSLog(@<span class="hljs-string">&quot;%d&quot;</span>,isHaveNums);
}


- (BOOL)twoNumSumWithTarget:(<span class="hljs-keyword">int</span>)target Array:(NSArray&lt;NSNumber *&gt; *)<span class="hljs-built_in">array</span> {

    NSMutableArray *finalArray = [NSMutableArray <span class="hljs-built_in">array</span>];

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">array</span>.count; i++) {

        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i + <span class="hljs-number">1</span>; j &lt; <span class="hljs-built_in">array</span>.count; j++) {

            <span class="hljs-keyword">if</span> ([<span class="hljs-built_in">array</span>[i] intValue] + [<span class="hljs-built_in">array</span>[j] intValue] == target) {

                [finalArray addObject:<span class="hljs-built_in">array</span>[i]];
                [finalArray addObject:<span class="hljs-built_in">array</span>[j]];
                NSLog(@<span class="hljs-string">&quot;%@&quot;</span>,finalArray);

                <span class="hljs-keyword">return</span> YES;
            }
        }
    }
    <span class="hljs-keyword">return</span> NO;
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Data-structure.html" class="navigation navigation-prev " aria-label="Previous page: 数据结构">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Foundation.html" class="navigation navigation-next " aria-label="Next page: Foundation">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"算法","level":"2.2","depth":1,"next":{"title":"Foundation","level":"3.1","depth":1,"path":"Foundation.md","ref":"Foundation.md","articles":[]},"previous":{"title":"数据结构","level":"2.1","depth":1,"path":"Data-structure.md","ref":"Data-structure.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"Arithmetic.md","mtime":"2020-04-13T10:23:32.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-05-27T09:12:21.691Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

